﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VoteSystem.Filters;
using VoteSystem.Services;

namespace VoteSystem.Models
{

    [SessionFilter]
    public class ConfigController : Controller
    {
        public PositionService positionService = new PositionService();
        public VoteService voteService = new VoteService();
        public ConfigService configService = new ConfigService();
        public AccountService accountService = new AccountService();
        //
        // GET: /Config/

        public ActionResult Import()
        {
            ViewBag.Positions = positionService.GetAllPositions();
            return View();
        }

        public ActionResult Export()
        {
            return View(configService.GetActiveVoteConfig());
        }

        public ActionResult ExportData()
        {
            var dt = voteService.QueryResult();
            string name =  "定聘表_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            string file = Server.MapPath("~/Results/" + name);
            ExcelHelper helper = new ExcelHelper(file);
            helper.DataTableToExcel(dt, "Sheet1", true);
           
            return File(file, "application/octet-stream",name );
        }

        public ActionResult ExportUnselectedData()
        {
            var dt = voteService.QueryUnSelectedResult();
            string name =  "落聘表_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            string file = Server.MapPath("~/Results/" + name);
            ExcelHelper helper = new ExcelHelper(file);
            helper.DataTableToExcel(dt, "Sheet1", true);
           
            return File(file, "application/octet-stream",name );
        }
        [HttpPost]
        public ActionResult ImportPosition(HttpPostedFileBase positionFile)
        {
            string fileName =Server.MapPath("~/Uploaded/"+ DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(positionFile.FileName)); 
            positionFile.SaveAs(fileName);

            ExcelHelper helper = new ExcelHelper(fileName);
            var dt = helper.ExcelToDataTable("Sheet1", true);
            var result = positionService.ImportPosition(dt);
            return View("Import", result);
        }

        public ActionResult ImportVoteData()
        {
            return View();
        }


        [HttpPost]
        public ActionResult ImportVoteData(HttpPostedFileBase voteFile)
        {
            string fileName =Server.MapPath("~/Uploaded/"+ DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(voteFile.FileName)); 
            voteFile.SaveAs(fileName);

            ExcelHelper helper = new ExcelHelper(fileName);
            var dt = helper.ExcelToDataTable("Sheet1", true);
            var result = positionService.ImportVoteData(dt);
            return View("ImportResult", result);
        }

        public ActionResult TimeConfig()
        {
            var config = configService.GetActiveVoteConfig();
            return View(config);
        }

        [HttpPost]
        public ActionResult TimeConfig(DateTime startTime1, DateTime endTime1,
                                       DateTime startTime2, DateTime endTime2,
                                       DateTime startTime3, DateTime endTime3)
        {
            var result = configService.ChangeVoteConfigDateTime(startTime1, endTime1, startTime2, endTime2, startTime3, endTime3);

            if (result.IsSuccess) 
            {
                ViewBag.IsSuccess = true;
                return View(configService.GetActiveVoteConfig());
            }
            else
            {
                throw new Exception("修改失败");
            }
        }

        public ActionResult ShowLoginUsers()
        {
            return View();
        }

        public ActionResult RemoveUser(string userName) 
        {
            accountService.LoginOff(userName);
            return Json(new OpResult() { IsSuccess = true});
        }

    }
}
